package ca.kooki.codesnippet.core;

import junit.framework.Assert;

import org.junit.Test;

public class XmlBufferTest {

  @Test
  public void SafeXmlNothingWrong()
  {
    String actual = XmlBuffer.safeXml("abc");
    Assert.assertEquals("abc",actual);
  }
  
  @Test
  public void SafeXml_And()
  {
    String actual = XmlBuffer.safeXml("&");
    Assert.assertEquals("&amp;",actual);
  }

  @Test
  public void SafeXml_SingleQuote()
  {
    String actual = XmlBuffer.safeXml("'");
    Assert.assertEquals("&apos;",actual);
  }

  @Test
  public void SafeXml_DoubleQuote()
  {
    String actual = XmlBuffer.safeXml("\"");
    Assert.assertEquals("&quot;",actual);
  }

  
  @Test
  public void SafeXml_GreaterThan()
  {
    String actual = XmlBuffer.safeXml(">");
    Assert.assertEquals("&gt;",actual);
  }

  @Test
  public void SafeXml_LessThan()
  {
    String actual = XmlBuffer.safeXml("<");
    Assert.assertEquals("&lt;",actual);
  }
  
  @Test
  public void AddEntity()
  {
    XmlBuffer buffer = new XmlBuffer();
    buffer.addEntity("book");
    
    String xml = buffer.toString();
    assertXmlEquals("<book", xml);
  }
  
  @Test
  public void CloseSimpleEntity()
  {
    XmlBuffer buffer = new XmlBuffer();
    buffer.addEntity("book");
    buffer.closeEntity();
    
    String xml = buffer.toString();
    assertXmlEquals("<book/>", xml);
  }

  @Test
  public void AddSimpleEntity()
  {
    XmlBuffer buffer = new XmlBuffer();
    buffer.addSimpleEntity("book","the firm");
    String xml = buffer.toString();
    assertXmlEquals("<book>the firm</book>", xml);
  }

  
  @Test
  public void CloseEntityWithInnerXml()
  {
    XmlBuffer buffer = new XmlBuffer();
    buffer.addEntity("book");
    buffer.addInnerXml("aha & blah");
    buffer.closeEntity();
    
    String xml = buffer.toString();
    assertXmlEquals("<book>aha &amp; blah</book>", xml);
  }

  @Test
  public void AddAttributes()
  {
    XmlBuffer buffer = new XmlBuffer();
    buffer.addEntity("book");
    buffer.addAttribute("id", "1");
    buffer.addAttribute("name", "agile");
    buffer.closeEntity();
    
    String xml = buffer.toString();
    assertXmlEquals("<book id=\"1\" name=\"agile\"/>", xml);
  }
  
  @Test
  public void MultipleEntities()
  {
    XmlBuffer buffer = new XmlBuffer();
    buffer.addEntity("book");
    buffer.addAttribute("id", "1");
    buffer.addAttribute("name", "agile");
    buffer.addEntity("author");
    buffer.addAttribute("name", "Beck");
    buffer.closeEntity();
    buffer.closeEntity();
    
    String xml = buffer.toString();
    assertXmlEquals("<book id=\"1\" name=\"agile\"><author name=\"Beck\"/></book>", xml);
  }
  
  @Test
  public void NoInnerXmlBug()
  {
    XmlBuffer buffer = new XmlBuffer();
    buffer.addEntity("all-users");
    buffer.addSimpleEntity("next-id","1");

    buffer.addEntity("user");
    buffer.addAttribute("id", "1");
    buffer.addAttribute("name", "aforward");
    buffer.closeEntity();

    buffer.addEntity("user");
    buffer.addAttribute("id", "2");
    buffer.addAttribute("name", "james");
    buffer.closeEntity();

    buffer.closeEntity();

    String expected = "<all-users><next-id>1</next-id><user id=\"1\" name=\"aforward\"/><user id=\"2\" name=\"james\"/></all-users>";
    assertXmlEquals(expected,buffer.toString());
  }
 
  
  private void assertXmlEquals(String expected, String actual)
  {
    String xmlExpected = "<?xml version=\"1.0\"?>" + expected;
    
    String xmlExpectedStripped = xmlExpected.replaceAll(" ", "");
    String actualStripped = actual.replaceAll(" ", "");

    xmlExpectedStripped = xmlExpectedStripped.replaceAll("\n", "");
    actualStripped = actualStripped.replaceAll("\n", "");

    Assert.assertEquals(xmlExpectedStripped, actualStripped);
  
  }
}
